package com.kandinfo.base.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtils {
    public static final String STRING_PATTERN_FULL = "yyyy-MM-dd HH:mm:ss";

    public static final String STRING_PATTERN_SHORT = "yyyy-MM-dd";

    public static final String DATE_PATTERN_FULL = "yyyyMMddHHmmss";

    public static final String DATE_PATTERN_YMD = "yyyyMMdd";

    public static final String DATE_PATTERN_HHMMSS = "HHmmss";

    public static final String DATE_PATTERN_HHMMSS_SSS = "HHmmssSSS";

    public static final String BETWEEN_DAY = "betweenDay";

    public static final String BETWEEN_HOUR = "betweenHour";

    public static final String BETWEEN_MIN = "betweenMin";

    public static final String BETWEEN_SEC = "betweenSec";

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);

    public static String getTodayForFile() {
        Date date = new Date();
        SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyyMMdd");
        return myFmt1.format(date);
    }

    public static Date getDate(Long time) {
        return new Date(time);
    }

    public static String getTimeDisplay(Long time) {
        String str = "";
        time = time / 1000;
        int s = (int) (time % 60);
        int m = (int) (time / 60 % 60);
        int h = (int) (time / 3600);
        str = h + "小时" + m + "分" + s + "秒";
        return str;
    }

    public static long getTimeCost(String fromDate, String toDate) throws ParseException {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long from = simpleFormat.parse(fromDate).getTime();
        long to = simpleFormat.parse(toDate).getTime();
        return (to - from) / 1000;
    }

    /**
     * 不可实例化
     */
    private DateUtils() {
    }

    /**
     * 获取Date型当前日期
     * 
     * @return
     */
    public static Date getCurrentDate() {
        return Calendar.getInstance().getTime();
    }

    /**
     * 获取Date型当前月份
     * 
     * @return
     */
    public static int getCurrentMonth() {
        Calendar c = new GregorianCalendar();
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * @param date
     * @return
     */
    public static String format(Date date) {
        FastDateFormat df = FastDateFormat.getInstance(STRING_PATTERN_FULL, null, null);
        return df.format(date);
    }

    /**
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        FastDateFormat df = FastDateFormat.getInstance(pattern, null, null);
        return df.format(date);
    }

    /**
     * 将传入的Date型日期转化为pattern类型的日期(默认使用yyyy-MM-dd HH:mm:ss格式)
     * 
     * @param date
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date getFormatDate(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            date = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            LOGGER.warn("ParseException ", e);
        }
        return date;
    }

    /**
     * 将传入的字符串日期转化为指定格式的Date型日期(默认使用yyyy-MM-dd HH:mm:ss格式)
     * 
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getFormatDateByString(String dateStr, String pattern) {
        if (StringUtils.isBlank(pattern)) {
            pattern = STRING_PATTERN_FULL;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            LOGGER.warn("ParseException ", e);
        }
        return date;
    }

    /**
     * 获取两个时间间差的时间
     * 
     * @param biggerDate
     * @param smallerDate
     * @return
     * @throws XylException
     */
    public static Map<String, Integer> getBetweenDateTime(Date biggerDate, Date smallerDate) throws Exception {
        if (biggerDate == null || smallerDate == null) {
            throw new Exception("Arguments could not be null.");
        }
        if (biggerDate.getTime() < smallerDate.getTime()) {
            throw new Exception("The first argument must be smaller than the second.");
        }
        long l = biggerDate.getTime() - smallerDate.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long sec = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put(BETWEEN_DAY, (int) day);
        map.put(BETWEEN_HOUR, (int) hour);
        map.put(BETWEEN_MIN, (int) min);
        map.put(BETWEEN_SEC, (int) sec);

        return map;
    }

}
